<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<title>购买</title>

		<meta name="viewport" content="initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<!-- 引入css文件 -->
		<link rel="stylesheet" href="../css/sm.min.css">
		<link rel="stylesheet" href="../css/sm-extend.min.css">
		<link rel="stylesheet" href="../css/iconfont.css">
		<style type="text/css">
			#index {
				margin-top: 0.4rem;
			}
			.infinite-scroll-preloader {
				margin-top: -20px;
			}
			/* 卡片内容加边距 */
			.my-cont{
				padding:.2rem .99rem;
				text-align:center
				
			}
			/* 背景色白色 */
			.page, .page-group{
				background-color: white;
			}
			/* 去掉卡片边距 */
			.card{
				/* margin: 0; */
			}
			.card-content{
				margin: 0.625rem;
			}
			/* 用户字体 */
			.list-block .my-user{
				font-size :0.6rem;
				color:rgb(150,150,150);
			}
			/* 头像圆角 */
			.my-photo img{
				border-radius: 1rem;
			}
			/*商品*/
			.commodity-title{
				font-weight: bold;
				font-size: medium;
			}
			.commodity-price{
				font-weight: bold;
				color: red;
				padding-right: 5rem;
				font-size: medium;
			}
			/* 用户 */
			.user-name{
				font-size :0.6rem;
				color:gray;
			}
			.leavemessage{
				font-size :0.6rem;
			}
			.link{
				font-size :0.6875rem;
				font-weight: bold;
			}
			/* 跳转连接颜色 */
			a{
				color: inherit;
			}
			a:active{
				color: inherit;
			}
			
			/* 卡片主内容左右边距 */
			.my-data{
				padding-right: 7rem;
			}
		</style>
	</head>
	<body>
		<!-- page 容器 -->
		<div class="page" id="app">
			<!-- 标题栏 -->
			<header class="bar bar-nav" style="background-color: white;"> 
				<a class="icon icon-left button button-link pull-left" @click="goBack" style="color: black;"></a>
				<h1 class="title" style="color: #000000;">购买宝贝</h1>
			</header>
			
			<!-- 工具栏 -->
			<nav class="bar bar-tab" v-if="commodity!=null">
				<div class="" style="width: 5rem;float: right;margin-right: 0.625rem;">
					<a @click="goOrder" class="button button-big button-fill button-danger">
						立即购买
					</a>
				</div>
				<div class="user-name" style="text-align: right;margin-top: 0.625rem;">
					实付款：<span class="commodity-price" v-if="commodity.freightStatus==1">￥{{commodity.price+10}}</span>
							<span class="commodity-price" v-if="commodity.freightStatus==0">￥{{commodity.price}}</span>
				</div>
			</nav>
			
			<!-- 页面内容 -->
			<div class="content" v-if="commodity!=null">
				<!-- 主内容 -->
				<div class="list-block">
					<div class="card list-container">
						<div class="list-block media-list inset ">
							<ul>
							  <li>
								  <div class="item-content">
									<div class="item-media">
										<img :src="imgSrc(commodity.picture)" width="60rem">
									</div>
									<div class="item-inner">
									  <div class="item-subtitle">{{commodity.title}}</div>
									</div>
								  </div>
								  <div class="item-content">
									  <div class="item-inner">
										<div class="item-title-row">
										  <div class="item-subtitle">
											  付款：<span class="commodity-price">￥{{commodity.price}}</span>
										  </div>
										</div>
										<div class="item-subtitle">
											运费：<span class="commodity-price" v-if="commodity.freightStatus==0">￥0</span>
													<span class="commodity-price" v-if="commodity.freightStatus==1">￥10</span>
										</div>
									  </div>
								  </div>
							  </li>
							</ul>
						</div>
					</div>
					<div class="card list-container">
						<div class="list-block media-list inset ">
							<ul>
							  <li>
								  <div class="item-content">
									  <div class="item-inner">
										<div class="item-subtitle">收货地址</div>
										<div class="item-input">
											<select v-model="myaddress">
												<option v-for="address in allAddress">
													{{address.user.nickname}} {{address.phone}} 
													{{address.shopAddress.country}}{{address.shopAddress.province}}{{address.shopAddress.city}}{{address.shopAddress.detail}}
												</option>
											</select>
										</div>
									  </div>
								  </div>
							  </li>
							</ul>
						</div>
					</div>
				</div>
			</div>
		</div>
		<script src='../js/jquery-3.2.1.min.js'></script>
		<script src='../js/sm.js'></script>
		<script src='../js/sm-extend.js'></script>
		<script src='../js/vue.js'></script>
		<script src='../js/axios.min.js'></script>
		<script src="../js/config.js"></script>
		
		<script type="text/javascript">
			//获取url中的参数
			function getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
			var r = window.location.search.substr(1).match(reg); //匹配目标参数
			if (r != null) return unescape(r[2]); return null; //返回参数值
			 }
		</script>
		
		<script>
			//获取URL参数
			var loginuserid = getUrlParam('loginuserid');
			var commodityid = getUrlParam('commodityid');
			
			//VUE
			var app = new Vue({
				el:"#app",
				data:{
					loginuser:null,
					commodity:null,
					allAddress:[],
					myaddress:null,
					order:{
						commodityId:null,
						freight:null,
						sumprice:null,
						senduserId:null,
						buyuserPhone:null,
						buyuserAddress:null,
						buyuserId:null
					}
				},
				created() {
					//初始加载登录用户
					this.initloginuser();
					//初始加载商品
					this.initcommodity();
				},
				// 方法
				methods:{
					initloginuser: function() {
						axios.get(serviceIP+"/market/inituser?userid=" + loginuserid,{
							headers:{"token":token,"refreshtoken":refreshtoken}
						}).then(result => {
							this.loginuser = result.data.data;
							//初始加载所有地址
							this.initalladdress();
						});
					},
					initcommodity: function() {
						axios.get(serviceIP+"/market/initcommodity?commodityid=" + commodityid,{
							headers:{"token":token,"refreshtoken":refreshtoken}
						}).then(result => {
							this.commodity = result.data.data;
						});
					},
					initalladdress: function() {
						axios.get(serviceIP+"/market/initalladdress?userId=" + loginuserid,{
							headers:{"token":token,"refreshtoken":refreshtoken}
						}).then(result => {
							this.allAddress = result.data.data;
						});
					},
					
					//跳转上层打开页面
					goBack:function(){
						window.history.go(-1)
					},
					//图片映射
					imgSrc: function(src) {
						return src;
					},
					//跳到购买页
					goOrder:function(){
						var _this = this;
						if(_this.myaddress==null||_this.myaddress==""){
							$.toast("请填写地址");
						}
						else{
							$.confirm('确定购买？', function() {
								//alert(userid+loginUser.id+attentionStatus);
								//确定购买赋值order对象
								if(_this.commodity.freightStatus==0){
									_this.order.freight=0;
								}else if(_this.commodity.freightStatus==1){
									_this.order.freight=10;
								}
								_this.order.commodityId=_this.commodity.id;
								_this.order.sumprice=_this.commodity.price+_this.order.freight;
								_this.order.senduserId=_this.commodity.user.id;
								_this.order.buyuserPhone=_this.myaddress.trim().split(" ")[1];
								_this.order.buyuserAddress=_this.myaddress.trim().split(" ")[2];
								_this.order.buyuserId=_this.loginuser.id;
								// console.log(_this.order);
								axios.get(serviceIP+"/market/insertorder", {
									params: {
										order: JSON.stringify(_this.order)
									},
									headers:{"token":token,"refreshtoken":refreshtoken}
								}).then(result => {
									if (result.data.code == 200) {
										$.toast("操作成功!");
										window.location="market-pay.html?orderId="+result.data.data.id
									} else {
										$.toast("操作被打断，请重试！");
									}
								});
							});
						}
					},
				},
			});
		</script>
		
		<script>
			$.init();
		</script>
	</body>

</html>
